﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.IO;

namespace WindowsFormsApplication1
{
	public partial class 系统唯一数 : Form
	{
		public 系统唯一数()
		{
			InitializeComponent();
		}
		private bool isNum = false;
		//数字+字母
		private const string CHAR1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		//纯数字
		private const string CHAR2 = "0123456789";
		private void btnCreate_Click(object sender, EventArgs e)
		{
            this.UseWaitCursor = true;
			Stopwatch sw = new Stopwatch();
			sw.Start();
			//设置是否是纯数字
			isNum = cbValue.Checked;
			//生成随机数,参数是字符长度和生成数量
			List<string> list = GetRandString((int)numLen.Value, (int)numCount.Value);
            sw.Stop();
            this.UseWaitCursor = false;
			if (list == null)
			{
				return;
			}

			string strInfo = string.Format("成功生成{1}个随机字符串！用时：{0}毫秒！", sw.ElapsedMilliseconds, numCount.Value);
			strInfo += "\r\n" + "是否查看生成结果？";

			DialogResult result = MessageBox.Show(strInfo, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
			if (result == DialogResult.OK)
			{
				StringBuilder str_content = new StringBuilder();
				foreach (string r in list)
				{
					str_content.Append(r + "\r\n");
				}

				string file_path = Application.StartupPath + "\\rand_" + DateTime.Now.ToFileTime().ToString() + ".txt";
				File.WriteAllText(file_path, str_content.ToString());
				Process.Start(file_path);
			}
		}

        /// <summary>
        /// 随机排序
        /// </summary>
        /// <param name="charList"></param>
        /// <returns></returns>
		private List<string> SortByRandom(List<string> charList)
		{
			Random rand = new Random();
			for (int i = 0; i < charList.Count; i++)
			{
				int index = rand.Next(0, charList.Count);
				string temp = charList[i];
				charList[i] = charList[index];
				charList[index] = temp;
			}
			return charList;
		}

		private void ShowError(string strError)
		{
			MessageBox.Show(strError, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
		}

        /// <summary>
        /// 获取随机字符串
        /// </summary>
        /// <param name="len">字符长度</param>
        /// <param name="count">生成数量</param>
        /// <returns></returns>
		private List<string> GetRandString(int len, int count)
		{
			double max_value = Math.Pow(36, len);
			if (max_value > long.MaxValue)
			{
				ShowError(string.Format("Math.Pow(36, {0}) 超出 long最大值！", len));
				return null;
			}
			
			long all_count = (long)max_value;
			long stepLong = all_count / count;
			if (stepLong > int.MaxValue)
			{
				ShowError(string.Format("stepLong ({0}) 超出 int最大值！", stepLong));
				return null;
			}
			int step = (int)stepLong;
			if (step < 3)
			{
				ShowError("step 不能小于 3!");
				return null;
			}
			long begin = 0;
			List<string> list = new List<string>();
			Random rand = new Random();
			while (true)
			{
				long value = rand.Next(1, step) + begin;
				begin += step;
				list.Add(GetChart(len, value, this.isNum));
				if (list.Count == count)
				{
					break;
				}
			}
			list = SortByRandom(list);
			return list;
		}

        /// <summary>
        /// 将数字转化成字符串
        /// </summary>
        /// <param name="len"></param>
        /// <param name="value"></param>
        /// <returns></returns>
		private string GetChart(int len, long value, bool isNum = false)
		{
			StringBuilder str = new StringBuilder();
			string CHARStr = isNum ? CHAR2 : CHAR1;
			while (true)
			{
				str.Append(CHARStr[(int)(value % CHARStr.Length)]);
				value = value / CHARStr.Length;
				if (str.Length == len)
				{
					break;
				}
			}
			return str.ToString();
		}
	}
}
